package com.filldream.fastboot.common.stockUtils.cffex;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpUtil;
import com.filldream.fastboot.common.constant.enums.StockFstEnum;
import com.filldream.fastboot.common.okutil.stock.cf.CfKLineUtil;
import com.filldream.fastboot.common.okutil.stock.cf.kline.GetKLineSTO;
import com.filldream.fastboot.common.okutil.stock.cf.kline.KLineItemDTO;
import com.filldream.fastboot.common.stockUtils.cffex.entity.StockIndexFuture;
import com.filldream.fastboot.common.stockUtils.cffex.entity.StockIndexFutureProductTypeEnum;
import com.filldream.fastboot.common.util.JsonUtil;
import com.filldream.fastboot.common.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import java.util.*;

/**
 * CFFEX期货数据获取
 */
@Slf4j
public class CffexUtil {

    public static void main(String[] args) {
        List<StockIndexFuture> indexFuture = getIndexFutureWithKLine("20250606", "IF");
        indexFuture.forEach(System.out::println);
    }


    /**
     * 获取指数期货的记录
     * @param dataStr 日期，格式:yyyyMMdd
     * @param productId   股指期指类型
     * @return  股指期货数据列表
     */
    public static  List<StockIndexFuture> getIndexFuture(String dataStr, String productId){
        dataStr = dataStr.replaceAll("-", "");
        final String yearMonth = dataStr.substring(0,6);
        final String day = dataStr.substring(6);
        List<StockIndexFuture> result = new ArrayList<>();
        StockIndexFutureProductTypeEnum stockIndexFutureProductTypeEnum = StockIndexFutureProductTypeEnum.findById(productId);
        if( stockIndexFutureProductTypeEnum != null ){
            String urlXml = HttpUtil.get("http://www.cffex.com.cn/sj/ccpm/"+yearMonth+"/"+day+"/"+stockIndexFutureProductTypeEnum.getDataTypeId()+".xml?id="+ RandomUtil.randomNumByInterval(1,100));
            if( urlXml.indexOf("302 Found") < 0 ){
                Map<String, Object> xmlMap = XmlUtil.xmlToMap(urlXml);
                result = JsonUtil.fromJosnArray(JsonUtil.toJson(xmlMap.get("data")), StockIndexFuture.class);
                result.forEach(item -> {
                    item.setProductName(stockIndexFutureProductTypeEnum.getRemark());
                    item.setSearchCode(stockIndexFutureProductTypeEnum.getSearchCode());
                });
            }
        }
        return result;
    }

    /**
     * 获取指数期货的记录,并且附带K线数据
     * @param dataStr 日期，格式:yyyyMMdd
     * @param productId   股指期指类型
     * @return  股指期货数据列表
     */
    public static  List<StockIndexFuture> getIndexFutureWithKLine(String dataStr, String productId){
        dataStr = dataStr.replaceAll("-", "");
        List<StockIndexFuture> list = getIndexFuture(dataStr,  productId);
        if(ObjectUtils.isNotEmpty(list)){
            String searchCode = list.get(0).getSearchCode();
            String dateTempStr = dataStr.substring(2, 4) + "-" + dataStr.substring(4,6) + "-" + dataStr.substring(6) ;
            GetKLineSTO getKLineSTO = new GetKLineSTO();
            getKLineSTO.setSearchCode(searchCode);
            getKLineSTO.setType(Integer.valueOf(StockFstEnum.dayK.getType()));
            List<KLineItemDTO> kLine = CfKLineUtil.getKLine(getKLineSTO).getList();
            KLineItemDTO kLineData = kLine.stream().filter(t -> t.getDate().equals(dateTempStr)).findFirst().orElse(null);
            if (kLineData != null) {
                list.forEach(item -> {
                    item.setKLineData(kLineData);
                });
            }
        }
        return list;
    }

}



